<#include "/java_copyright.include">
<#assign className = table.className>
<#assign classNameLower = className?uncap_first>
package ${basePackage}.controller.${module};

import jakarta.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

import ${basePackage}.common.constant.StaticMsg;
import ${basePackage}.controller.base.BaseController;
import ${basePackage}.po.${module}.${className}PO;
import ${basePackage}.common.base.R;
import ${basePackage}.service.${module}.${className}Service;
import ${basePackage}.common.exception.MyException;
import com.ns.school.common.config.SecurityUtil;
import java.util.List;

/**
<#include "/java_description.include">
 */
@Slf4j
@RestController
@RequestMapping("${module}")
public class ${className}Controller extends BaseController {

    @Autowired
    private ${className}Service ${classNameLower}Service;

    /**
     * 查询列表
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "get${className}List")
    public R<List<?>> list(HttpServletRequest request, ${className}PO params) {
        try {
            return ${classNameLower}Service.queryPageList(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 查询单个详情
     *
     * @param request
     * @param params
     * @return
     */
    @RequestMapping(value = "get${className}Info")
    public R<?> info(HttpServletRequest request, ${className}PO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            return R.success(${classNameLower}Service.queryById(params));
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 添加
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("insert${className}")
    public R<?> insert(HttpServletRequest request, ${className}PO params) {
        try {
            Integer userId = SecurityUtil.id();
            params.setCreateBy(userId);
            return ${classNameLower}Service.insert(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 更新
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("update${className}")
    public R<?> update(HttpServletRequest request, ${className}PO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            Integer userId = SecurityUtil.id();
            params.setUpdateBy(userId);
            return ${classNameLower}Service.update(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

    /**
     * 删除
     *
     * @param request
     * @param params
     * @return
     */
    @PostMapping("updateDel${className}")
    public R<?> updateDel(HttpServletRequest request, ${className}PO params) {
        try {
            if (isEmpty(params.getEncryptionId())) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            Integer id = decode(params.getEncryptionId());
            if (id == null) {
                return R.error(StaticMsg.MSG_ERROR_000);
            }
            params.setId(id);
            Integer userId = SecurityUtil.id();
            params.setUpdateBy(userId);
            return ${classNameLower}Service.updateDel(params);
        } catch (MyException e) {
            MyException.print(log, e);
            return R.error(e.getMessage());
        } catch (Exception e) {
            MyException.print(log, e);
            return R.error(StaticMsg.MSG_ERROR_000);
        }
    }

}
